package com.example.sunsg.okhttptest.customview;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.PointF;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.breadtrip.R;
import com.example.sunsg.okhttptest.util.DisplayUtils;

/**
 * Created by sunsg on 15/12/4.
 */
public class PullToZoonListView extends ListView{
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    /**
     * 要滑动的iamge
     */
    private ImageView mImageView;
    private RelativeLayout mHeaderView;

    /**存储 touch的 x 和 y值*/
    private  final PointF mTouchPoint = new PointF();
    /**判断是滑动还是点击的临界值*/
    private int mTouchSlop;
    /**headview的初始高度*/
    private float mHeadViewInitHeight;
    /**headerview 可拉伸的最大高度*/
    private float mHeadViewMaxHeight;
    private int mActionPointerId = -1;

    public PullToZoonListView(Context context) {
        this(context,null);
    }

    public PullToZoonListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    /**
     * 初始化
     * @param context
     */
    private void init(Context context){
        this.mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
        initHeadView();

        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    /**
     * 初始化HeadView
     */
    private void initHeadView(){
        mHeaderView = (RelativeLayout) mLayoutInflater.inflate(R.layout.view_pull_to_zoon_listview_headview,this,false);

        //ImageView
        mImageView = ((ImageView) mHeaderView.findViewById(R.id.im_bg));
        mImageView.setImageResource(R.drawable.ren);

        //动态设置headerview高
        int displayWidth = DisplayUtils.getScreenWidth(mContext);
        int showHeight = displayWidth*3/4;
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(displayWidth,showHeight);
        mHeaderView.setLayoutParams(params);
        addHeaderView(mHeaderView);

        //headerview 的初始高度 和 headerveiw可拉伸的最大高度
        mHeadViewInitHeight = showHeight;
        float maxScal = DisplayUtils.getScreenHeight(mContext)/mHeadViewInitHeight;
        mHeadViewMaxHeight = mHeadViewInitHeight * maxScal;

//        Log.i("test","initheight = "+mHeadViewInitHeight);
//        Log.i("test","maxHeight = "+mHeadViewMaxHeight);
    }

    /**
     * 添加headview
     * @param view
     */
    public void addCustomView(View view){
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        view.setLayoutParams(params);
        mHeaderView.addView(view);
    }


    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        final float x = ev.getX();
        final float y = ev.getY();
        switch(ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                mTouchPoint.x = x;
                mTouchPoint.y = y;
//                mActionPointerId = ev.getPointerId(0);
                break;
            case MotionEvent.ACTION_MOVE:
                if(!canScrollUp()){
                    ViewGroup.LayoutParams params = mHeaderView.getLayoutParams();
                    if(params.height <= mHeadViewMaxHeight && params.height >= mHeadViewInitHeight){
                        params.height = (int)(ev.getY() - mTouchPoint.y + params.height);
                        params.height = Math.max(params.height,(int)mHeadViewInitHeight);
                        params.height = Math.min(params.height,(int)mHeadViewMaxHeight);
                        mHeaderView.setLayoutParams(params);
                        mTouchPoint.y = ev.getY();
                        return true;
                    }
                }
                mTouchPoint.y = ev.getY();
                break;
            case MotionEvent.ACTION_UP:
                Log.i("test","action_up");
                scrollToInit();
                resetInit();
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
//                Log.i("test","ACTION_POINTER_DOWN");
//                int i = ev.getActionIndex();
//                this.mTouchPoint.y = ev.getY(i);
//                this.mActionPointerId = ev.getPointerId(i);
                break;
            case MotionEvent.ACTION_POINTER_UP:
                Log.i("test","ACTION_POINTER_UP");
//                int n = (ev.getAction()) >> 8;
//                if (ev.getPointerId(n) == this.mActionPointerId)
//                    if (n != 0) {
//                        this.mTouchPoint.y = ev.getY(0);
//                        this.mActionPointerId = ev.getPointerId(0);
////                        return;
//                    }
                break;
        }
        return super.onTouchEvent(ev);
    }

    public boolean canScrollUp() {
        if (android.os.Build.VERSION.SDK_INT < 14) {
            return getChildCount() > 0
                    && (getFirstVisiblePosition() > 0 || getChildAt(0).getTop() < getPaddingTop());
        } else {
            return ViewCompat.canScrollVertically(this, -1);
        }
    }

    /**使headview回到初始位置*/
    public void scrollToInit(){
        ValueAnimator animator = ValueAnimator.ofFloat((float)mHeaderView.getBottom(),mHeadViewInitHeight);
        animator.setDuration(200);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(final ValueAnimator animation) {
                float current =(float)(animation.getAnimatedValue());
                ViewGroup.LayoutParams params = mHeaderView.getLayoutParams();
                params.height = (int)current;
                mHeaderView.setLayoutParams(params);
            }
        });
        animator.start();
    }

    /**
     * 恢复一些初始值
     */
    private void resetInit(){
        mActionPointerId = -1;
    }
}
